package com.diamond.service.impl;

import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;

import com.diamond.bo.MessageBO;
import com.diamond.bo.QueryMessageBO;
import com.diamond.bo.ReturnBO;
import com.diamond.entity.Message;
import com.diamond.po.MessagePO;
import com.diamond.service.IMessageService;

/**
 * @author JSmart Tools
 */
@SuppressWarnings("unchecked")
public class MessageService extends Service<MessagePO> implements IMessageService {

	public void addOrUpdateMessage(MessagePO messagePo) throws Exception {
		Message message = new Message();
		PropertyUtils.copyProperties(message, messagePo);
		super.genericDAO.saveOrUpdate(message);
	}

	public ReturnBO<MessageBO> queryMessage(QueryMessageBO queryMessageBo)
			throws Exception {
		ReturnBO<MessageBO> returnBo = new ReturnBO<MessageBO>();
		StringBuilder queryMessageSql = new StringBuilder("select distinct * from message where 1=1 and message.del_status=0");
		StringBuilder queryTotalSql = new StringBuilder("select distinct * from message where 1=1 and message.del_status=0");
		
		if (null != queryMessageBo.getAuthor() && !"".equals(queryMessageBo.getAuthor())){
			queryMessageSql.append(" and message.author like '%").append(queryMessageBo.getAuthor()).append("%'");
			queryTotalSql.append(" and message.author like '%").append(queryMessageBo.getAuthor()).append("%'");
		}
		if (null != queryMessageBo.getMsgTitle() && !"".equals(queryMessageBo.getMsgTitle())) {
			queryMessageSql.append(" and message.name like '%").append(queryMessageBo.getMsgTitle()).append("%'");
			queryTotalSql.append(" and message.name like '%").append(queryMessageBo.getMsgTitle()).append("%'");
		}
		if (null != queryMessageBo.getMsgType() && !"".equals(queryMessageBo.getMsgType())) {
			queryMessageSql.append(" and message.msg_type='").append(queryMessageBo.getMsgType()).append("'");
			queryTotalSql.append(" and message.msg_type='").append(queryMessageBo.getMsgType()).append("'");
		}
		if (null != queryMessageBo.getDateFrom() && !"".equals(queryMessageBo.getDateFrom())
				&& null != queryMessageBo.getDateTo() && !"".equals(queryMessageBo.getDateTo())) {
			queryMessageSql.append(" and message.utime between '").append(queryMessageBo.getDateFrom())
			.append("' and '").append(queryMessageBo.getDateTo()).append("'");
			queryTotalSql.append(" and message.utime between '").append(queryMessageBo.getDateFrom())
			.append("' and '").append(queryMessageBo.getDateTo()).append("'");
		}
		queryMessageSql.append(" order by message.utime desc").append(" limit ").append(queryMessageBo.getStart()).append(", ").append(queryMessageBo.getPageSize());
		List<MessageBO> messageBolist = super.genericDAO.findBySqlToObj(queryMessageSql.toString(), MessageBO.class);
		List totlelist = super.genericDAO.findBySqlToObj(queryTotalSql.toString(), null);
		returnBo.setReturnBolist(messageBolist);
		returnBo.setTotalSize(totlelist.size());
		returnBo.setStart(queryMessageBo.getStart());
		returnBo.setPageSize(queryMessageBo.getPageSize());
		return returnBo;
	}

	public Boolean checkMsgTitle(String msgTitle) throws Exception {
		Boolean result = false;
		String sql = "select * from message where 1=1 and message.del_status=0 and message.name='" + msgTitle+ "'";
		List<MessageBO> list = super.genericDAO.findBySqlToObj(sql, MessageBO.class);
		if (list.size() == 0) {
			result = true;
		}
		return result;
	}

	public MessageBO findMessageByMessageId(String messageId) throws Exception {
		StringBuilder sql = new StringBuilder("select * from message where 1=1 and message.del_status=0 and message.message_id=").append(messageId);
		List<MessageBO> messageBolist = super.genericDAO.findBySqlToObj(sql.toString(), MessageBO.class);
		if (messageBolist.size() > 0) {
			return messageBolist.get(0);
		} else {
			return null;
		}
	}

	public Boolean deleteMessage(String[] messageIds) throws Exception {
		Boolean result = false;
		StringBuilder delSql = new StringBuilder("update message set message.del_status=1 where message.message_id in (");
		for (int i=0; i<messageIds.length; i++) {
			if (i < messageIds.length - 1) {
				delSql.append(messageIds[i]).append(",");
			} else {
				delSql.append(messageIds[i]).append(")");
			}
		}
		Integer i = super.genericDAO.updateBySql(delSql.toString());
		if (i > 0) {
			result = true;
		}
		return result;
	}
}
